package org.mtmi.io.controls;

import java.io.IOException;

import net.n3.nanoxml.IXMLElement;

import org.mtmi.io.ReaderError;
import org.mtmi.io.SceneReader;
import org.mtmi.io.SceneWriter;
import org.mtmi.io.XMLReader;
import org.mtmi.io.XMLWriter;
import org.mtmi.ui.controls.AbstractRangeValueControl;
import org.mtmi.ui.controls.Control;
import org.mtmi.ui.controls.IValueGenerator;
import org.mtmi.ui.controls.Slider;
import org.mtmi.ui.controls.TrackPad;

/**
 * In addition to {@link AbstractRangeValueControl}, save {@link Slider} features:
 * <ul>
 * <li> the dot color.
 * <li> the knob color.
 * </ul>
 */
public class TrackPadIO extends ControlIO
{
	
	private static final String E_HAXIS = "hAxis"; //$NON-NLS-1$
	private static final String E_VAXIS = "hAxis"; //$NON-NLS-1$
	private static final String A_MIN = "min"; //$NON-NLS-1$
	private static final String A_MAX = "max"; //$NON-NLS-1$

	public TrackPadIO() {
		setProcessFeature( F_BACKGROUND_COLOR, true );
		setProcessFeature( F_FOREGROUND_COLOR, true );
	}
	
	@Override
	public void writeControl( Control c, XMLWriter w, SceneWriter sw ) throws IOException 
	{
		super.writeControl(c, w, sw);
		
		//values:
		TrackPad t = (TrackPad)c;
		IValueGenerator v = t.getHAxis();
		w.addElement( E_HAXIS, A_MIN, v.getMinValue(), A_MAX, v.getMaxValue() );
		v = t.getVAxis();
		w.addElement( E_VAXIS, A_MIN, v.getMinValue(), A_MAX, v.getMaxValue() );
	}
	
	@Override
	public void readControl( Control c, IXMLElement element, XMLReader r, SceneReader sr ) throws ReaderError 
	{
		super.readControl(c, element, r, sr);
		
		TrackPad t = (TrackPad)c;
		//values:
		IXMLElement e = r.getElement( element, E_HAXIS, false );
		if( e!=null ) {
			int min = r.getAttr( e, A_MIN, 0 );
			int max = r.getAttr( e, A_MAX, 100 );
			
			t.getHAxis().setRange( min, max );
		}
		e = r.getElement( element, E_VAXIS, false );
		if( e!=null ) {
			int min = r.getAttr( e, A_MIN, 0 );
			int max = r.getAttr( e, A_MAX, 100 );
			
			t.getVAxis().setRange( min, max );
		}
	}
}
